/**
 * 
 */
package com.admissions.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.admissions.R;
import com.admissions.common.Constants;
import com.admissions.diemchuan.TraDiemChuan;
import com.admissions.model.TruongModel;
import com.admissions.tylechoi.TyLeChoi;

/**
 * @author LongNH
 *
 */
/**
 * @author LongNH
 * 
 */
public class TruongAdapter extends BaseAdapter {
	private List<TruongModel> listTruong;
	private String type;
	/**
	 * The activity of Updates screen.
	 */
	private Activity activity;
	/**
	 * The inflater layout of list view.
	 */
	private LayoutInflater inflater = null;

	public TruongAdapter(Activity ac, List<TruongModel> listTruong, String type) {
		activity = ac;
		this.type = type;
		this.listTruong = listTruong;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		try {
			return listTruong.size();
		} catch (Exception e) {
			return 0;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * Class holds the View of Adapter.
	 * 
	 */
	public static class ViewHolder {
		public TextView tvName;
		private LinearLayout layout;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder = null;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.item, null);
			holder = new ViewHolder();
			holder.tvName = (TextView) vi.findViewById(R.id.tv_name);
			holder.layout = (LinearLayout) vi.findViewById(R.id.item);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		holder.tvName.setText(listTruong.get(position).getName());
		holder.layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				
				if (Constants.TYPE_TY_LE_CHOI.equals(type)) {
					TyLeChoi.idTruong = listTruong.get(position).getId();
					if (TyLeChoi.alertDialog.isShowing()) {
						TyLeChoi.alertDialog.dismiss();
						TyLeChoi.tvTruong.setText(listTruong.get(position)
								.getName());
					}
				}else {
					if (Constants.TYPE_DIEM_CHUAN.equals(type)) {
						TraDiemChuan.idTruong = listTruong.get(position).getId();
						if (TraDiemChuan.alertDialog.isShowing()) {
							TraDiemChuan.alertDialog.dismiss();
							TraDiemChuan.tvTruong.setText(listTruong.get(position)
									.getName());
						}
					}
					
				}
					
				
			}
		});

		return vi;
	}
}
